﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">


  <!-- Begin : Switch current node to its related template -->
  <xsl:template name="SwitchToManifestTemplate">
    <xsl:param name="application" />
    <xsl:param name="server" />

    <xsl:variable name="templateName" select="@template" />
    <xsl:variable name="elementName" select="name()" />

    <xsl:if test="$templateName != ''">
      <xsl:apply-templates select="//templates/template[@type=$elementName and @name= $templateName]/*">
        <xsl:with-param name="application" select="$application" />
        <xsl:with-param name="server" select="$server" />
      </xsl:apply-templates>
    </xsl:if>

  </xsl:template>
  <!-- End : Switch current node to its related template -->

  <!-- Begin : copy template values on current node-->
  <xsl:template match="template" mode="deploymentV2.0">
    <xsl:param name="current" />

    <xsl:variable name="templateNode" select="./*"/>
    <xsl:variable name="templateNodeName" select="name($templateNode)"/>

    <xsl:attribute name="template">
      <xsl:value-of select="$templateNode/@name"/>
    </xsl:attribute>

    <!--Add attribute from template then from server-->
    <xsl:apply-templates select="$templateNode/@*" mode="copyAttribute" />
    <xsl:apply-templates select="$current/@*" mode="copyAttribute" />


    <!--browse sub-elements-->
    <xsl:apply-templates select="$templateNode/*" mode="deploymentV2.0">
      <xsl:with-param name="current" select="$templateNode" />
    </xsl:apply-templates>


  </xsl:template>
  <!-- End : copy template values on current node-->


  <!-- Begin : Node copy -->
  <xsl:template match="*" mode="deploymentV2.0">
    <xsl:param name="current" />

    <xsl:variable name="templateNode" select="."/>
    <xsl:variable name="templateNodeName" select="name($templateNode)"/>

    <xsl:element name="{$templateNodeName}">
      <xsl:choose>
        <xsl:when test="$templateNode/@template != ''">
          <xsl:apply-templates select="//template[@name=$templateNode/@template and @type=$templateNodeName]" mode="deploymentV2.0">
            <xsl:with-param name="current" select="$templateNode" />
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>

          <!--Add attribute from template then from server-->
          <xsl:apply-templates select="$templateNode/@*" mode="copyAttribute" />
          <!--<xsl:apply-templates select="$current/@*" mode="copyAttribute" />-->

          <!--browse sub-elements-->
          <xsl:apply-templates select="$templateNode/*" mode="deploymentV2.0">
            <xsl:with-param name="current" select="$templateNode" />
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
  </xsl:template>
  <!-- End : Node copy -->
</xsl:stylesheet>
